package com.hitqz.robot.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hitqz.robot.api.business.entity.CvdVoiceEntity;
import com.hitqz.robot.api.business.dto.CvdVoiceQueryDTO;
import com.hitqz.robot.api.business.dto.CvdVoiceSaveDTO;
import com.hitqz.robot.api.business.dto.CvdVoiceUpdateDTO;
import com.hitqz.robot.api.business.vo.CvdVoiceVO;
import com.hitqz.robot.biz.mapper.CvdVoiceMapper;
import com.hitqz.robot.biz.service.CvdVoiceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import lombok.RequiredArgsConstructor;

import java.util.List;

/**
 * 迎宾讲解语音包
 *
 * @author kehanjiang
 * @date 2025-09-25 09:53:18
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CvdVoiceServiceImpl extends ServiceImpl<CvdVoiceMapper, CvdVoiceEntity> implements CvdVoiceService {


    private CvdVoiceVO convertToVO(Object o) {
        if (o instanceof CvdVoiceEntity) {
            CvdVoiceVO vo = new CvdVoiceVO();
            BeanUtil.copyProperties(o, vo);
            //设置其他参数

            return vo;
        }
        return null;
    }

    private void preCheck(CvdVoiceEntity entity, Boolean update) {
        Long id = entity.getId();
        //String name = entity.getName();
        if (update) {//更新操作
            Assert.notNull(id, "id不可为空！");
        } else {
            //Assert.hasText(name, "name不可为空！");
        }
    }

    @Override
    public Page<CvdVoiceVO> getCvdVoicePage(Page page, CvdVoiceQueryDTO dto) {
        CvdVoiceEntity entity = new CvdVoiceEntity();
        BeanUtils.copyProperties(dto, entity);
        entity.setName(null);
        final String name = dto.getName();
        LambdaQueryWrapper<CvdVoiceEntity> wrapper = new LambdaQueryWrapper<>(entity);
        wrapper.like(StringUtils.hasText(name), CvdVoiceEntity::getName, name);
        wrapper.orderByAsc(CvdVoiceEntity::getId);
        return (Page<CvdVoiceVO>) this.page(page, wrapper)
                .convert(this::convertToVO);
    }

    @Override
    public CvdVoiceVO getCvdVoiceById(Long id) {
        Assert.notNull(id, "id不可为空！");
        return convertToVO(this.getById(id));
    }


    @Override
    public CvdVoiceEntity saveCvdVoice(CvdVoiceSaveDTO dto) {
        CvdVoiceEntity entity = new CvdVoiceEntity();
        BeanUtils.copyProperties(dto, entity);
        this.preCheck(entity, false);
        this.save(entity);
        return entity;
    }

    @Override
    public Boolean updateCvdVoiceById(CvdVoiceUpdateDTO dto) {
        CvdVoiceEntity entity = new CvdVoiceEntity();
        BeanUtils.copyProperties(dto, entity);
        this.preCheck(entity, true);

        CvdVoiceEntity oldEntity = this.getById(entity.getId());
        String oldContent = oldEntity.getContent();
        String newContent = entity.getContent();
        if (!oldContent.equals(newContent)) {
            entity.setMediaStatus("0");
        }
        return this.updateById(entity);
    }

    @Override
    public Boolean removeCvdVoiceBatchByIds(List<Long> ids) {
        Assert.notEmpty(ids, "ID不可为空！");
        return this.removeBatchByIds(ids);
    }

}